//
//  ANOptionsView.m
//  Ants
//
//  Created by ZHM on 16/4/28.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANOptionsView.h"

@interface ANOptionsView ()

@property (nonatomic, weak) UIButton *okBtn;
@property (nonatomic, weak) UIButton *noBtn;
@property (nonatomic, weak) UIButton *mengView;
@end

#define optionsViwHeight 89

@implementation ANOptionsView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addUpAllChildView];
        self.userInteractionEnabled = YES;
        self.image = [UIImage imageWithStretchableName:@"timeline_card_bottom_line_highlighted"];
        [self setMangView];
        [ANKeyWindow addSubview:self];
    }
    return self;
}

+ (instancetype)optionsViewWithTitle:(NSString *)title delegate:(id)delegate {
    ANOptionsView *optionsView = [[ANOptionsView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(ANKeyWindow.frame), ANKeyWindow.bounds.size.width, optionsViwHeight)];
    optionsView.delegate = delegate;
    optionsView.showString = title;
    return optionsView;
}

- (void)setMangView {
    //蒙版视图
    UIButton *mengView = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
    mengView.backgroundColor = [UIColor blackColor];
    mengView.alpha = 0;
    [mengView addTarget:self action:@selector(clickMengView) forControlEvents:UIControlEventTouchUpInside];
    _mengView = mengView;
    [ANKeyWindow addSubview:mengView];
    
    //动画
    [UIView animateWithDuration:0.25 animations:^{
        mengView.alpha = 0.4;
        self.y = CGRectGetMaxY(mengView.frame) - optionsViwHeight;
    }];
}
//取消选项视图
- (void)clickMengView {
    [UIView animateWithDuration:0.25 animations:^{
        self.mengView.alpha = 0;
        self.y = CGRectGetMaxY(_mengView.frame);
    } completion:^(BOOL finished) {
        [self.mengView removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)addUpAllChildView {
    //确定
    UIButton *okBtn = [[UIButton alloc] init];
    
    [okBtn setBackgroundColor:[UIColor orangeColor]];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [okBtn setBackgroundImage:[UIImage imageWithStretchableName:@"tabbar_background"] forState:UIControlStateHighlighted];
    _okBtn = okBtn;
    [okBtn addTarget:self action:@selector(chickOk) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:okBtn];
    //取消
    UIButton *noBtn = [[UIButton alloc] init];
    [noBtn setTitle:@"取消" forState:UIControlStateNormal];
    [noBtn setBackgroundColor:[UIColor whiteColor]];
    [noBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [noBtn setBackgroundImage:[UIImage imageWithStretchableName:@"tabbar_background"] forState:UIControlStateHighlighted];
    _noBtn = noBtn;
    [noBtn addTarget:self action:@selector(chickNo) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:noBtn];
}
- (void)chickOk {
    if ([self.delegate respondsToSelector:@selector(optionsView:didClickBtn:)]) {
        [self.delegate optionsView:self didClickBtn:1];
        [self clickMengView];
    }
}
- (void)chickNo {
    if ([self.delegate respondsToSelector:@selector(optionsView:didClickBtn:)]) {
        [self.delegate optionsView:self didClickBtn:2];
        [self clickMengView];
    }
}
- (void)layoutSubviews {
    [_okBtn setTitle:self.showString forState:UIControlStateNormal];
    [super layoutSubviews];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.width;
    CGFloat h = 44;
    _okBtn.frame = CGRectMake(x, y, w, h);
    
    CGFloat no_x = 0;
    CGFloat no_y = self.height - 44;
    CGFloat no_w = self.width;
    CGFloat no_h = 44;
    _noBtn.frame = CGRectMake(no_x, no_y, no_w, no_h);
}

@end
